package com.abso.mp3tunes.locker.ui.dialogs;

import java.util.List;

import org.apache.commons.lang.ObjectUtils;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TrayDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.abso.mp3tunes.locker.core.data.AccountData;
import com.abso.mp3tunes.locker.core.data.LockerDataUtils;
import com.abso.mp3tunes.locker.core.data.AccountData.Subscription;

/**
 * A specialized dialog showing the Locker account data.
 */
public class AccountDataDialog extends TrayDialog {

	/** The Locker account data. */
	private AccountData accountData;

	/**
	 * Constructs a new dialog.
	 * 
	 * @param accountData
	 *            the Locker account data.
	 * @param parent
	 *            the parent shell.
	 */
	public AccountDataDialog(AccountData accountData, Shell parent) {
		super(parent);
		this.accountData = accountData;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
	 */
	protected void configureShell(Shell shell) {
		super.configureShell(shell);
		shell.setText("Locker Account");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
	 */
	protected Control createDialogArea(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		composite.setLayout(new GridLayout(1, true));
		createAccountArea(composite);
		createSubscriptionsArea(composite);
		return composite;
	}

	/**
	 * Creates the contents of the upper part of the dialog, showing the account
	 * information.
	 * 
	 * @param parent
	 *            the parent composite.
	 */
	private void createAccountArea(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		composite.setLayout(new GridLayout(2, false));
		createNameAndValue(composite, "E-mail:", accountData.getEmail());
		createNameAndValue(composite, "Nickname:", accountData.getNickName());
		createNameAndValue(composite, "First Name:", accountData.getFirstName());
		createNameAndValue(composite, "Last Name:", accountData.getLastName());
		long maxLockerSize = accountData.getMaxLockerSize();
		if (maxLockerSize == -1L) {
			createNameAndValue(composite, "Max Locker Size:", "Unlimited");
		} else {
			createNameAndValue(composite, "Max Locker Size:", LockerDataUtils
					.getPrettyPrintedSize(maxLockerSize));
		}
		createNameAndValue(composite, "Current Locker Size:", LockerDataUtils
				.getPrettyPrintedSize(accountData.getCurrentLockerSize()));
		long maxFileSize = accountData.getMaxFileSize();
		if (maxFileSize == -1L) {
			createNameAndValue(composite, "Max File Size:", "Unlimited");
		} else {
			createNameAndValue(composite, "Max File Size:", LockerDataUtils
					.getPrettyPrintedSize(maxFileSize));
		}
		createNameAndValue(composite, "Locker Type:", accountData
				.getLockerType());
		createNameAndValue(composite, "Expired Status:", Integer
				.toString(accountData.getExpired()));
	}

	/**
	 * Creates the contents of the lower part of the dialog, showing
	 * subscriptions.
	 * 
	 * @param parent
	 *            the parent composite.
	 */
	private void createSubscriptionsArea(Composite parent) {
		List subscriptions = accountData.getSubscriptions();
		if (subscriptions.size() == 1) {
			createSubscriptionGroup(parent,
					(Subscription) subscriptions.get(0), "Subscription");
		} else {
			for (int i = 0; i < subscriptions.size(); i++) {
				Subscription subscription = (Subscription) subscriptions.get(i);
				createSubscriptionGroup(parent, subscription, "Subscription "
						+ (i + 1));
			}
		}
	}

	/**
	 * Creates a composite group showing a subscription.
	 * 
	 * @param parent
	 *            the parent composite.
	 * @param subscription
	 *            the subscription being shown.
	 * @param title
	 *            the group's title.
	 */
	private void createSubscriptionGroup(Composite parent,
			Subscription subscription, String title) {
		Group group = new Group(parent, SWT.NONE);
		group.setText(title);
		group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		group.setLayout(new GridLayout(2, false));
		createNameAndValue(group, "Name:", subscription.getName());
		createNameAndValue(group, "Description:", subscription.getDescription());
		createNameAndValue(group, "Activation Date:", ObjectUtils
				.toString(subscription.getActivateDate()));
		createNameAndValue(group, "Expiration Date:", ObjectUtils
				.toString(subscription.getExpireDate()));
	}

	/**
	 * Creates a name/value controls.
	 * 
	 * @param parent
	 *            the parent composite.
	 * @param name
	 *            the name to be shown.
	 * @param value
	 *            the value to be shown.
	 */
	private void createNameAndValue(Composite parent, String name, String value) {
		Label nameLabel = new Label(parent, SWT.NONE);
		nameLabel.setText(name);
		nameLabel
				.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
		Text valueText = new Text(parent, SWT.BORDER);
		valueText.setEditable(false);
		valueText.setText(value);
		valueText
				.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
	 */
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
				true);
	}

}
